class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        for (auto& e : nums)
        {
            if (e == 0)
                e = -1;
        }
        unordered_map<int, int> m;
        int pre = 0;
        int max = 0;
        m[0] = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            pre += nums[i];
            if (m.count(pre) != 0)
            {
                max = max > (i + 1) - m[pre] ? max : (i + 1) - m[pre];
            }
            else
            {
                m[pre] = i + 1;
            }

        }
        return max;
    }
};